<?php

header( 'Content-Type: text/plain' );

require_once dirname( __FILE__ ) . '/config.php';

if ( !empty( $_SERVER['REMOTE_ADDR'] ) && $_SERVER['REMOTE_ADDR'] != ADMIN_IP )
	die( 'The cleanup command can only be initiated by the server admin' );

$chats = glob( dirname( __FILE__ ) . '/db/chat/*.txt' );

foreach ( $chats as $chat ) {
	if ( filesize( $chat ) <= 1 && filemtime( $chat ) < time() - TIMEOUT_MESSAGE && fileatime( $chat ) < time() - TIMEOUT_VISITED && basename( $chat ) != 'public.txt' ) {
		unlink( $chat );
		unlink( dirname( __FILE__ ) . '/db/online/' . basename( $chat ) );
		echo 'Deleted #' . substr( basename( $chat ), 0, -4 ) . "\n";
	} else {
		echo 'Kept #' . substr( basename( $chat ), 0, -4 ) . "\n";
	}
}

?>